<?php
class CountController extends AdminBase{
	//广告统计
	public function actionAd(){
		$this->assign($_GET);
		$this->baseIndex('CountAdModel');
	}
	//商品统计
	public function actionGoods(){

        $objModel = new CountGoodsModel('admin');
        $objModel->unsetAttributes();
        if(isset($_GET['CountGoodsModel']))
        {
            $objModel->attributes = $_GET['CountGoodsModel'];
        }

        $objCriteria = $objModel->createSearchCriteria();

       
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //信息总数
        $count = $objModel->count($objCriteria);


        $count = $objModel->count($objCriteria);
        //分页
        $page = self::page($count, 20, $pageId,'',$objCriteria);

        //排序
        
        $objSort = new CSort('CountGoodsModel');
        $objSort->applyOrder($objCriteria);






        $data = $objModel->findAll($objCriteria);
        //销量总数
        $objCriteria->select = 'sum(Deal) as Deal';
        $dangSum = $objModel->find($objCriteria)->Deal;
        $objCriteria->select = 'sum(Sales) as Sales';
        $zongSales = $objModel->find($criteria)->Sales;


        $this->assign('dangSum', $dangSum);
        $this->assign('zongSales', $zongSales);        
        $this->assign("Page", $page->show());
        $this->assign('data', $data);
        $this->assign('sort', $objSort);
        $this->assign('model', $objModel);
        $this->assign('count', $count);   
        $this->render();  
	}
	//来路分析统计
	public function actionReferer(){
		$this->assign($_GET);
		$this->baseIndex('CountRefererModel');
	}
	//销量管理
	public function actionSalesData(){

        $objModel = new SalesDataModel('admin');
        $objModel->unsetAttributes();
        if(isset($_GET['SalesDataModel']) || isset($_GET['key']))
        {
        	if(isset($_GET['key'])){
        		switch ($_GET['key']) {
        			case '1':
        				$objModel->TGoodsName = $_GET['keyword'];
        				break;
        			case '2':
        				$objModel->TProductID = $_GET['keyword'];
        				break;
        			case '3':
        				$objModel->GoodsID = $_GET['keyword'];
        				break;
        			case '4':
        				$objModel->TOrderNumber = $_GET['keyword'];
        			case '4':
        				$objModel->GoodsBmID = $_GET['keyword'];
        				break;       			
        			default:
        				# code...
        				break;
        		}
        	}
            $objModel->attributes = $_GET['SalesDataModel'];
        }

        $objCriteria = $objModel->createSearchCriteria();

       
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //信息总数
        $count = $objModel->count($objCriteria);


        $count = $objModel->count($objCriteria);
        //分页
        $page = self::page($count, 20, $pageId,'',$objCriteria);

        //排序
        
        $objSort = new CSort('SalesDataModel');
        $objSort->applyOrder($objCriteria);






        $data = $objModel->findAll($objCriteria);      
        $this->assign("Page", $page->show());
        $this->assign('data', $data);
        $this->assign('sort', $objSort);
        $this->assign('model', $objModel);
        $this->assign('count', $count);   
        $this->render();  

	}
	//导入销量
    //导入
    public function actionImport() {
        $model = SalesDataModel::model();
        if (IS_POST) {
            if (empty($_FILES['file']['name'])) {
                $this->error("请选择上传文件！");
            }
            //上传临时文件地址
            $filename = $_FILES['file']['tmp_name'];
            if (strtolower(substr($_FILES['file']['name'], -3, 3)) != 'csv') {
                $this->error("上传的文件格式有误！");
            }
            if ($model->excelImport($filename)) {
                $source = (int) $_POST['source'];
                $this->success('数据读取成功，开始导入数据！', self::U('import', array('act' => 'import', 'source' => $source)));
            } else {
                $this->error('数据导入失败！');
            }
        } else {
            $action = $_GET['act'];
            $count = (int) $_GET['count'];
            if ($action == 'import') {
                $source = (int) Yii::app()->request->getParam('source');
                $key = 'SalesDataModel_excelImport';
                $memcache = new Memcache();
                $memcache->connect('127.0.0.1', 11211);
                $dataList = $memcache->get($key);
                if (empty($dataList)) {
                    $this->success('导入完成1！', self::U('import'));
                    exit;
                }
                $sum = $_GET['sum'] ? $_GET['sum'] : count($dataList);
                $i = 0;
                foreach ($dataList as $k => $rs) {
                    $model->import($rs, $source);
                    unset($dataList[$k]);
                    $i++;
                    $count++;
                    if ($i >= 100) {
                        if (empty($dataList)) {
                            $memcache->delete($key);
                        } else {
                            $memcache->set($key, $dataList, 0, 600);
                        }
                        $this->success("数据导入中....，已导入：{$count}，总数：{$sum}", self::U('import', array('count' => $count, 'act' => 'import', 'sum' => $sum, 'source' => $source)));
                        exit;
                    }
                }
                $this->success('导入完成2！', self::U('import'));
            } else {
                $this->render();
            }
        }
    }
}

